package com.njit.office.controller;

import com.njit.office.form.AddTaskForm;
import com.njit.office.model.Result;
import com.njit.office.model.TaskModel;
import com.njit.office.model.TaskState;
import com.njit.office.service.TaskService;

import com.njit.office.service.TeamService;
import com.njit.office.utils.IDUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/api/tasks/")
public class TaskController {

    @Resource
    TaskService taskService;

    @Resource
    TeamService teamService;


    @PostMapping("add")
    @ResponseBody
    public Result addTask(@ModelAttribute("addTaskForm")AddTaskForm addTaskForm){

        return taskService.add(addTaskForm);

    }

    @GetMapping("update/urgent/{taskId}")
    @ResponseBody
    public Result urgentTask(@PathVariable("taskId")String taskId){

        return taskService.urgentTask(taskId);
    }

    @GetMapping("update/finish/{taskId}")
    @ResponseBody
    public Result finishTask(@PathVariable("taskId")String taskId){

        return taskService.updateTaskState(taskId,TaskState.FINISHED);
    }
    @GetMapping("update/del/{taskId}")
    @ResponseBody
    public Result delTask(@PathVariable("taskId")String taskId){
        return taskService.deleteTask(taskId);
    }

    @GetMapping("getTeamUsers/{teamId}")
    public String getTeamUsers(@PathVariable("teamId") String teamId, Model model) {
        Result res = teamService.getAllMembers(teamId);
        model.addAttribute("teamUsers", res.getData());

        return "task::teamUsersFragment";
    }

    @GetMapping("updateView/doingTasks")
    public String updateView1(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("userId") == null) {
            return "task::doingTasksFragment";
        }
        String userId = (String) session.getAttribute("userId");
        Result res = taskService.getTask(userId);
        List<TaskModel> taskModels = (List<TaskModel>) res.getData();

        //过滤正在进行并且不是紧急的任务
        List<TaskModel> doingTasks = taskModels.stream().filter(new Predicate<TaskModel>() {
            @Override
            public boolean test(TaskModel taskModel) {
                return taskModel.getTaskState().equals(TaskState.UNFINISHED)&&!taskModel.isUrgent();
            }
        }).collect(Collectors.toList());
        model.addAttribute("doingTasks", orderByDeadLine(doingTasks));

        return "task::doingTasksFragment";
    }

    @GetMapping("updateView/finishedTasks")
    public String updateView(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("userId") == null) {
            return "task::finishedTasksFragment";
        }
        String userId = (String) session.getAttribute("userId");
        Result res = taskService.getTask(userId);
        List<TaskModel> taskModels = (List<TaskModel>) res.getData();

        //过滤已完成的任务
        List<TaskModel> doingTasks = taskModels.stream().filter(new Predicate<TaskModel>() {
            @Override
            public boolean test(TaskModel taskModel) {
                return taskModel.getTaskState().equals(TaskState.FINISHED);
            }
        }).collect(Collectors.toList());
        model.addAttribute("finishedTasks", orderByDeadLine(doingTasks));

        return "task::finishedTasksFragment";
    }

    @GetMapping("updateView/urgentTasks")
    public String updateView2(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("userId") == null) {
            return "task::urgentTasksFragment";
        }
        String userId = (String) session.getAttribute("userId");
        Result res = taskService.getTask(userId);
        List<TaskModel> taskModels = (List<TaskModel>) res.getData();

        //过滤正在进行并且不是紧急的任务
        List<TaskModel> doingTasks = taskModels.stream().filter(new Predicate<TaskModel>() {
            @Override
            public boolean test(TaskModel taskModel) {
                return taskModel.getTaskState().equals(TaskState.UNFINISHED)&&taskModel.isUrgent();
            }
        }).collect(Collectors.toList());
        model.addAttribute("urgentTasks", orderByDeadLine(doingTasks));

        return "task::urgentTasksFragment";
    }


    private List<TaskModel> orderByDeadLine(List<TaskModel> taskModels) {

        List<TaskModel> collect = taskModels.stream().sorted(new Comparator<TaskModel>() {
            @Override
            public int compare(TaskModel o1, TaskModel o2) {
                long l = o1.getDeadLine().getTime() - o2.getDeadLine().getTime();
                if (l == 0) {
                    return 0;
                } else if (l > 0) {
                    return -1;
                } else {
                    return 1;
                }
            }
        }).collect(Collectors.toList());
        return collect;
    }
}
